package com.ly.dingtalk.service.callback.bot;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

import com.dingtalk.api.request.OapiRobotSendRequest;
import com.ly.dingtalk.component.application.IDingBot;
import com.ly.dingtalk.pojo.callback.DingBotMsgCallbackRequest;
import com.ly.dingtalk.service.IDingBotMsgCallBackHandler;

@Component
public class MessageTypeCallbackHandler implements IDingBotMsgCallBackHandler<String> {
    
    private static final Pattern PATTERN = Pattern.compile("^\\s*(文本|链接|Markdown|整体跳转actionCard|独立跳转actionCard|feedCard)$");

    @Override
    public Predicate<DingBotMsgCallbackRequest> predication() {
        return payload ->  PATTERN.asPredicate().test(payload.getText().getContent());
    }

    @Override
    public String beforeMessageSend(IDingBot app, DingBotMsgCallbackRequest payload) {
        payload.setConversationType("2");
        return "前置处理完成：返回测试数据";
    }

    @Override
    public OapiRobotSendRequest handleMessageToDirect(IDingBot app, DingBotMsgCallbackRequest payload, String data) {
        if ("文本".equals(payload.getText().getContent().trim())) {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent("这是一个Text文本消息");
            request.setText(text);
            return request;
        }
        
        if ("链接".equals(payload.getText().getContent().trim())) {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("link");
            OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
            link.setTitle("这是Link消息");
            link.setText("这是一个Link消息");
            link.setMessageUrl("https://open.dingtalk.com/document/");
            link.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
            request.setLink(link);
            return request;
        }
        
        if ("Markdown".equals(payload.getText().getContent().trim())) {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("markdown");
            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
            markdown.setTitle("这是一个Markdown消息");
            markdown.setText("#### 杭州天气 @150XXXXXXXX  \n  > 9度，西北风1级，空气良89，相对温度73%  \n  > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)  \n  > ###### 10点20分发布 [天气](https://open.dingtalk.com/document/)  \n  ");
            request.setMarkdown(markdown);
            return request;
        }
        
        if ("整体跳转actionCard".equals(payload.getText().getContent().trim())) {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("actionCard");
            OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
            actioncard.setTitle("整体跳转actionCard消息");
            actioncard.setText("![这是一张图片](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n  " +
                    "这是一个整体跳转actionCard消息");
            actioncard.setSingleTitle("阅读全文");
            actioncard.setSingleURL("https://open.dingtalk.com/document/");
            request.setActionCard(actioncard);
            return request;
        }
        
        if ("独立跳转actionCard".equals(payload.getText().getContent().trim())) {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("actionCard");
            OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
            actioncard.setTitle("独立跳转actionCard消息");
            actioncard.setText("![这是一张图片](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n  这是一个独立跳转actionCard消息");
            OapiRobotSendRequest.Btns btnsOne = new OapiRobotSendRequest.Btns();
            btnsOne.setTitle("查看摘要");
            btnsOne.setActionURL("https://open.dingtalk.com/document/");
            OapiRobotSendRequest.Btns btnsTwo = new OapiRobotSendRequest.Btns();
            btnsTwo.setTitle("不感兴趣");
            btnsTwo.setActionURL("https://open.dingtalk.com/");
            List<OapiRobotSendRequest.Btns> btnsList = new ArrayList<>();
            btnsList.add(btnsOne);
            btnsList.add(btnsTwo);
            actioncard.setBtns(btnsList);
            actioncard.setBtnOrientation("1");
            request.setActionCard(actioncard);
            return request;
        }
        
        if ("feedCard".equals(payload.getText().getContent().trim())) {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("feedCard");
            OapiRobotSendRequest.Feedcard feedcard = new OapiRobotSendRequest.Feedcard();
            OapiRobotSendRequest.Links linkOne = new OapiRobotSendRequest.Links();
            linkOne.setTitle("这是feedcard消息1");
            linkOne.setMessageURL("https://open.dingtalk.com/document/");
            linkOne.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
            OapiRobotSendRequest.Links linkTwo = new OapiRobotSendRequest.Links();
            linkTwo.setTitle("这是feedcard的消息2");
            linkTwo.setMessageURL("https://open.dingtalk.com/");
            linkTwo.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
            OapiRobotSendRequest.Links linkThree = new OapiRobotSendRequest.Links();
            linkThree.setTitle("这是feedcard的消息3");
            linkThree.setMessageURL("https://www.dingtalk.com/");
            linkThree.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
            List<OapiRobotSendRequest.Links> linksList = new ArrayList<>();
            linksList.add(linkOne);
            linksList.add(linkTwo);
            linksList.add(linkThree);
            feedcard.setLinks(linksList);
            request.setFeedCard(feedcard);
            return request;
        }
        
        return IDingBotMsgCallBackHandler.super.handleMessageToDirect(app, payload, data);
    }

}
